/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.rec.be;

import edu.upn.sigemfi.seg.bc.PersonaJuridicaFacade;
import edu.upn.sigemfi.seg.bc.PersonaNaturalFacade;
import edu.upn.sigemfi.seg.be.Persona;
import edu.upn.sigemfi.seg.be.PersonaJuridica;
import edu.upn.sigemfi.seg.be.PersonaNatural;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Diego
 */
@Entity
@Table(name = "REC_CREDITO")
@NamedQueries({
    @NamedQuery(name = "Credito.findAll", query = "SELECT c FROM Credito c"),
    @NamedQuery(name = "Credito.findByIdCredito", query = "SELECT c FROM Credito c WHERE c.idCredito = :idCredito"),
    @NamedQuery(name = "Credito.findByFechaRegistro", query = "SELECT c FROM Credito c WHERE c.fechaRegistro = :fechaRegistro"),
    @NamedQuery(name = "Credito.findByMontoTotal", query = "SELECT c FROM Credito c WHERE c.montoTotal = :montoTotal"),
    @NamedQuery(name = "Credito.findByFechaInicio", query = "SELECT c FROM Credito c WHERE c.fechaInicio = :fechaInicio"),
    @NamedQuery(name = "Credito.findByFechaFin", query = "SELECT c FROM Credito c WHERE c.fechaFin = :fechaFin")})
public class Credito implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID_CREDITO", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idCredito;
    @Column(name = "FECHA_REGISTRO")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaRegistro;
    @Column(name = "MONTO_TOTAL", precision = 53)
    private Double montoTotal;
    @Column(name = "FECHA_INICIO")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaInicio;
    @Column(name = "FECHA_FIN")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaFin;
    @Column(name = "ESTADO")
    private Boolean estado;
    @OneToMany(mappedBy = "fidRecCredito", fetch = FetchType.EAGER)
    private List<CuotaCredito> cuotaCreditoCollection;
    @OneToMany(mappedBy = "fidRecCredito", fetch = FetchType.EAGER)
    private List<ProcesoCobranza> procesoCobranzaCollection;
    @JoinColumn(name = "FID_REC_CARTERA", referencedColumnName = "ID_CARTERA")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Cartera fidRecCartera;
    @JoinColumn(name = "FID_SEG_PERSONA", referencedColumnName = "ID_PERSONA")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Persona fidSegPersona;
//    private String cliente;



    public Credito() {
    }

    public Credito(Long idCredito) {
        this.idCredito = idCredito;
    }

    public Long getIdCredito() {
        return idCredito;
    }

    public void setIdCredito(Long idCredito) {
        this.idCredito = idCredito;
    }

    public Date getFechaRegistro() {
        return fechaRegistro;
    }

    public int getNroCuotas() {
        return this.getCuotaCreditoCollection().size();
    }

    public void setFechaRegistro(Date fechaRegistro) {
        this.fechaRegistro = fechaRegistro;
    }

    public Double getMontoTotal() {
        return montoTotal;
    }

    public void setMontoTotal(Double montoTotal) {
        this.montoTotal = montoTotal;
    }

    public Date getFechaInicio() {
        return fechaInicio;
    }

    public void setFechaInicio(Date fechaInicio) {
        this.fechaInicio = fechaInicio;
    }

    public Date getFechaFin() {
        return fechaFin;
    }

    public void setFechaFin(Date fechaFin) {
        this.fechaFin = fechaFin;
    }

    public Persona getFidSegPersona() {
        return fidSegPersona;
    }

    public void setFidSegPersona(Persona fidSegPersona) {
        this.fidSegPersona = fidSegPersona;
    }

//    public String getCliente(){
//        try {
//            PersonaNatural perN = (PersonaNatural) this.fidSegPersona;
//            PersonaJuridica perj = (PersonaJuridica) this.fidSegPersona;
//            if (perN.getNombrePersona() != null)
//                this.cliente =  perN.getApePaternoPersona() + " "+perN.getApeMaternoPersona() + ", "+perN.getNombrePersona();
//            else
//                this.cliente = perj.getRazonSocial();
//        } catch (Exception e) {
//            e.getMessage();
//        }
//        return this.cliente;
//    }

    public List<CuotaCredito> getCuotaCreditoCollection() {
        return cuotaCreditoCollection;
    }

    public void setCuotaCreditoCollection(List<CuotaCredito> cuotaCreditoCollection) {
        this.cuotaCreditoCollection = cuotaCreditoCollection;
    }

    public List<ProcesoCobranza> getProcesoCobranzaCollection() {
        return procesoCobranzaCollection;
    }

    public void setProcesoCobranzaCollection(List<ProcesoCobranza> procesoCobranzaCollection) {
        this.procesoCobranzaCollection = procesoCobranzaCollection;
    }

    public Cartera getFidRecCartera() {
        return fidRecCartera;
    }

    public void setFidRecCartera(Cartera fidRecCartera) {
        this.fidRecCartera = fidRecCartera;
    }

    public Boolean getEstado() {
        return estado;
    }

    public void setEstado(Boolean estado) {
        this.estado = estado;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idCredito != null ? idCredito.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Credito)) {
            return false;
        }
        Credito other = (Credito) object;
        if ((this.idCredito == null && other.idCredito != null) || (this.idCredito != null && !this.idCredito.equals(other.idCredito))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.upn.sigemfi.rec.be.Credito[idCredito=" + idCredito + "]";
    }
}
